﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;

namespace ORS.OrderRobot.Domain
{
    /// <summary>
    /// 系统配置
    /// </summary>
    public class SystemConfig : Entity<int>
    {
        private readonly List<ProductInfo> defaultProductInfo = new()
        {
                new() { Id = "2215100085", Name = "山崎18年" },
                new() { Id = "2215100081", Name = "响21年" },
                new() { Id = "2215100080", Name = "响30年" },
                //new() { Id = "3401011120", Name = "魅惑焕彩润唇膏" },
        };
        private readonly MointerTaskConfig defaultRefreshTokenTask = new()
        {
            StartTime = new TimeOnly(06, 0, 0),
            EndTime = new TimeOnly(18, 0, 0),
            Interval = 1500,
        };
        private readonly ProductMointerTaskConfig defaultMointerProductTask = new()
        {
            Interval = 1,
            Disabled = false,
        };
        private readonly AutoShoppingTaskConfig defaultAutoShoppingTask = new()
        {
            StartTime = new TimeOnly(07, 0, 0),
            EndTime = new TimeOnly(18, 0, 0),
            Interval = 1,
        };
        private readonly Dictionary<string, object> defaultRequestHeader = new Dictionary<string, object>
        {
            {"xweb_xhr",1},
            {"channel-id","WECHATMP" },
            {"x-dw-client-id","f9360ff8-23e9-4135-b344-2fbfb8402ff3" },
            {"user-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF MacWechat/3.8.7(0x13080712) UnifiedPCMacWechat(0xf264051b) XWEB/13910" },
            {"sec-fetch-site","cross-site" },
            {"sec-fetch-mode","cors" },
            {"sec-fetch-dest","empty" },
            {"referer","https://servicewechat.com/wx36ac2fc711abe3c5/32/page-frame.html" }
        };
        /// <summary>
        /// 商品特征配置
        /// </summary>
        public string MointerProductJson { get; set; } = string.Empty;
        [NotMapped]
        public List<ProductInfo> MointerProductInfos
        {
            get => string.IsNullOrEmpty(MointerProductJson) ? defaultProductInfo : JsonSerializer.Deserialize<List<ProductInfo>>(MointerProductJson) ?? defaultProductInfo;
            set => MointerProductJson = JsonSerializer.Serialize(value);
        }
        /// <summary>
        /// 商品监测任务配置
        /// </summary>
        public string MointerProductTaskJson { get; set; } = string.Empty;

        [NotMapped]
        public ProductMointerTaskConfig MointerProductTask
        {
            get => string.IsNullOrEmpty(MointerProductTaskJson) ? defaultMointerProductTask : JsonSerializer.Deserialize<ProductMointerTaskConfig>(MointerProductTaskJson) ?? defaultMointerProductTask;
            set => MointerProductTaskJson = JsonSerializer.Serialize(value);
        }

        /// <summary>
        /// 自动下单
        /// </summary>
        public string AutoShoppingTaskJson { get; set; } = string.Empty;
        [NotMapped]
        public AutoShoppingTaskConfig AutoShoppingTask
        {
            get => string.IsNullOrEmpty(AutoShoppingTaskJson) ? defaultAutoShoppingTask : JsonSerializer.Deserialize<AutoShoppingTaskConfig>(AutoShoppingTaskJson) ?? defaultAutoShoppingTask;
            set => AutoShoppingTaskJson = JsonSerializer.Serialize(value);
        }

        /// <summary>
        /// 用户登录任务配置
        /// </summary>
        public string RefreshTokenTaskJson { get; set; } = string.Empty;
        [NotMapped]
        public MointerTaskConfig RefreshTokenTask
        {
            get => string.IsNullOrEmpty(RefreshTokenTaskJson) ? defaultRefreshTokenTask : JsonSerializer.Deserialize<MointerTaskConfig>(RefreshTokenTaskJson) ?? defaultRefreshTokenTask;
            set => RefreshTokenTaskJson = JsonSerializer.Serialize(value);
        }
        /// <summary>
        /// api网关地址
        /// </summary>
        public string ApiGateway { get; set; } = "https://www.kixdutyfree.jp/s/KixDutyFree/dw/shop/v23_2";
        /// <summary>
        /// 目标站点所需的clientid参数
        /// </summary>
        public string ApiParamClientId { get; set; } = "f9360ff8-23e9-4135-b344-2fbfb8402ff3";
        /// <summary>
        /// 公共预设请求头
        /// </summary>
        public string PresetRequestHeaderJson { get; set; } = string.Empty;
        [NotMapped]
        public Dictionary<string,object> PresetRequestHeader
        {
            get => string.IsNullOrEmpty(PresetRequestHeaderJson) ? defaultRequestHeader : JsonSerializer.Deserialize<Dictionary<string,object>>(PresetRequestHeaderJson) ?? defaultRequestHeader;
            set => PresetRequestHeaderJson = JsonSerializer.Serialize(value);
        }
        /// <summary>
        /// 最近更新时间
        /// </summary>
        public DateTime LatestUpdateTime  {get;set;}
        
    }
}
